//
//  XCMySongListViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/19.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCMySongListViewController.h"
#import "UIImageView+WebCache.h"
#import "XCShareMusicTableViewCell.h"
#import "WDGDatabaseTool.h"
#import "OnlineAVplay.h"
#import "XCPlayViewController.h"
#import "XCSingleBottumView.h"
@interface XCMySongListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, assign) NSInteger songListIndex;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;

@property (nonatomic, assign) NSInteger numberOfSong;


@end

@implementation XCMySongListViewController
- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_dataSource release];
    [_tableView release];
    [_mutableArraySingerNAme release];
    [_nameArray release];
    [_mutableArraySongName release];
    [_mutableHeadIVPicurl release];
    
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    WDGDatabaseTool *myLove = [WDGDatabaseTool DBManageWithTableName:@"myLove"];
    [myLove openDatabase];
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    
    self.dataSource = [NSMutableArray arrayWithArray:[[myLove orderWithDictionary:@{@"id":@"desc"}] selectAllData]];
    
    [myLove closeDatabase];
    for (NSDictionary *dic in self.dataSource) {
        [self.mutableArraySongName addObject:[dic objectForKey:@"url"]];
        [self.mutableArraySingerNAme addObject:[dic objectForKey:@"singername"]];
        [self.mutableHeadIVPicurl addObject:[dic objectForKey:@"picurl"]];
        [self.nameArray addObject:[dic objectForKey:@"songname"]];
    }
    
    [self.tableView reloadData];
    
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
   
    [self MyTableViewBuild];
    [XCSingleBottumView shareBottomViewBuild].hidden=YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
//    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%lu", (unsigned long)self.dataSource.count] forKey:@"number"];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)MyTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    [self.view addSubview:self.tableView];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    NSDictionary *dic = self.dataSource[indexPath.row];
    cell.singerNameLable.text = [dic objectForKey:@"singername"];
    cell.nameLable.text = [dic objectForKey:@"songname"];
    [cell.picUrlImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picurl"]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];

    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    
    NSDictionary *dic = self.dataSource[indexPath.row];
    [[OnlineAVplay shareAVPlayer] playWithUrl:[dic objectForKey:@"url"]];
    [[OnlineAVplay shareAVPlayer] play];
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 7;
}
#pragma mark - UITableViewEdit

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
//确定编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
}
//是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
}
// 完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
        

        WDGDatabaseTool *myLove = [WDGDatabaseTool DBManageWithTableName:@"myLove"];
        [myLove openDatabase];
        NSDictionary *dic = self.dataSource[indexPath.row];
        //生成一个where条件
        WDGWhereCondition *deleteCondition = [WDGWhereCondition conditionWithColumnName:@"songname" Operator:@"=" Value:[dic objectForKey:@"songname"]];
        //根据条件删除
        if ([myLove deleteDataWithWhereCondition:deleteCondition])
            NSLog(@"删除成功");
        else
            NSLog(@"删除失败");
        
        [self.dataSource removeObjectAtIndex:indexPath.row];
        
        [self.nameArray removeObjectAtIndex:indexPath.row];
        [self.mutableHeadIVPicurl removeObjectAtIndex:indexPath.row];
        [self.mutableArraySingerNAme removeObjectAtIndex:indexPath.row];
        [self.mutableArraySongName removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    
        
    }
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
    
}

@end
